package com.gmall.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DirectExchange_Queue_Config {

    public final static String DIRECT_EXCHANGE = "direct_exchange";
    // 声明队列
    public final static String DIRECT_EX_QUEUE_1 = "direct_queue_1";

    public final static String DIRECT_EX_QUEUE_2 = "direct_queue_2";


    public final static String DIRECT_EX_QUEUE_3 = "direct_queue_3";

    public final static String DIRECT_EX_QUEUE_SMS_ROUTINGKEY = "sms";

    public final static String DIRECT_EX_QUEUE_EMAIL_ROUTINGKEY = "email";

    public final static String DIRECT_EX_QUEUE_QQ_ROUTINGKEY = "qq";


    // 声明交换机
    @Bean(DIRECT_EXCHANGE)
    public Exchange directExchange(){
        //Builder 设计模式
        return ExchangeBuilder.directExchange(DIRECT_EXCHANGE)
                .durable(true)//默认RabbitMQ 重启以后， 交换机就删掉，除非是durable=true
                .build();
    }

    // 声明队列
    @Bean(DIRECT_EX_QUEUE_1)
    public Queue directQueueSms(){
        //return new Queue(DIRECT_EX_QUEUE_SMS,true);
        return QueueBuilder.durable(DIRECT_EX_QUEUE_1).build();
    }

    @Bean(DIRECT_EX_QUEUE_2)
    public Queue directQueueEmail(){
        return QueueBuilder.durable(DIRECT_EX_QUEUE_2).build();
    }

    @Bean(DIRECT_EX_QUEUE_3)
    public Queue directQueueQQ(){
        return QueueBuilder.durable(DIRECT_EX_QUEUE_3).build();
    }

    @Bean("directBinding1")
    public Binding directBinding1(@Qualifier(DIRECT_EX_QUEUE_1) Queue queue,
                                    @Qualifier(DIRECT_EXCHANGE) Exchange exchange){

        return BindingBuilder.bind(queue).to(exchange).with(DIRECT_EX_QUEUE_SMS_ROUTINGKEY).noargs();
    }

    @Bean("directBinding2")
    public Binding directBinding2(@Qualifier(DIRECT_EX_QUEUE_2) Queue queue,
                                    @Qualifier(DIRECT_EXCHANGE) Exchange exchange){

        return BindingBuilder.bind(queue).to(exchange).with(DIRECT_EX_QUEUE_EMAIL_ROUTINGKEY).noargs();
    }

    @Bean("directBinding3")
    public Binding directBinding3(@Qualifier(DIRECT_EX_QUEUE_3) Queue queue,
                                    @Qualifier(DIRECT_EXCHANGE) Exchange exchange){

        return BindingBuilder.bind(queue).to(exchange).with(DIRECT_EX_QUEUE_QQ_ROUTINGKEY).noargs();
    }

    @Bean("directBindingSMS_1")
    public Binding directBindingSMS_1(@Qualifier(DIRECT_EX_QUEUE_3) Queue queue,
                                   @Qualifier(DIRECT_EXCHANGE) Exchange exchange){

        return BindingBuilder.bind(queue).to(exchange).with(DIRECT_EX_QUEUE_SMS_ROUTINGKEY).noargs();
    }

}
